﻿namespace WMS6.Web.Analytics
{
    using System;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.AnalyticsReportServiceRef;

    public class FinanceSalesDetailByDateRptList : BasePage
    {
        protected Button btnExport;
        protected Button btnList;
        protected Button btnRefresh;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F15321;
        protected GridView gvFinaceSalesDetailReportByCustomer;
        protected GridView gvFinanceSalesDetailReport;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnShowLot;
        protected HtmlHead Head1;
        protected Label lblFinanceSalesDetailReportList;
        protected Label lblMsg;
        protected HtmlLink mycss;
        protected ScriptManager ScriptManager1;
        private decimal TotalExtendedPrice;
        private decimal TotalQty;
        private decimal TotalUnitPrice;
        protected UpdatePanel up1;

        public void BindData2gvDiffCountReport(FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry)
        {
            DataSet ds = AnalyticsReportServiceClientProxy.GetInstance().GetFinanceSalesDetailByDateRptDataSet(financeSalesDetailRptQueryEntiry);
            this.CreateGridView(ds, financeSalesDetailRptQueryEntiry.BY_CUSTOMER);
            if (financeSalesDetailRptQueryEntiry.BY_CUSTOMER == "Y")
            {
                base.BindData2GridView1(this.gvFinaceSalesDetailReportByCustomer, ds);
            }
            else
            {
                base.BindData2GridView1(this.gvFinanceSalesDetailReport, ds);
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            int i;
            FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry = this.GetFinanceSalesDetailRptQueryEntiry();
            financeSalesDetailRptQueryEntiry.IsGetAll = true;
            this.hdnActionID.Value = "EXPORT";
            if (financeSalesDetailRptQueryEntiry.BY_CUSTOMER == "Y")
            {
                for (i = this.gvFinaceSalesDetailReportByCustomer.Columns.Count; 12 < i; i--)
                {
                    this.gvFinaceSalesDetailReportByCustomer.Columns.RemoveAt(i - 1);
                }
                this.gvFinaceSalesDetailReportByCustomer.AllowSorting = false;
            }
            else
            {
                for (i = this.gvFinanceSalesDetailReport.Columns.Count; 12 < i; i--)
                {
                    this.gvFinanceSalesDetailReport.Columns.RemoveAt(i - 1);
                }
                this.gvFinanceSalesDetailReport.AllowSorting = false;
            }
            string currentPageIndex = financeSalesDetailRptQueryEntiry.CurrentPage;
            financeSalesDetailRptQueryEntiry.CurrentPage = "0";
            this.BindData2gvDiffCountReport(financeSalesDetailRptQueryEntiry);
            financeSalesDetailRptQueryEntiry.IsGetAll = false;
            financeSalesDetailRptQueryEntiry.CurrentPage = currentPageIndex;
            this.SetFinanceSalesDetailRptQueryEntiry(financeSalesDetailRptQueryEntiry);
            if (financeSalesDetailRptQueryEntiry.BY_CUSTOMER == "Y")
            {
                this.gvFinaceSalesDetailReportByCustomer.ShowFooter = false;
                base.Export1Excel("FinanceSalesDetailRpt.xls", this.gvFinaceSalesDetailReportByCustomer);
            }
            else
            {
                this.gvFinanceSalesDetailReport.ShowFooter = false;
                base.Export1Excel("FinanceSalesDetailRpt.xls", this.gvFinanceSalesDetailReport);
            }
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry = this.GetFinanceSalesDetailRptQueryEntiry();
            financeSalesDetailRptQueryEntiry.OwnerID = "";
            financeSalesDetailRptQueryEntiry.CustomerID = "";
            financeSalesDetailRptQueryEntiry.SkuID = "";
            financeSalesDetailRptQueryEntiry.StartDatetime = "";
            financeSalesDetailRptQueryEntiry.EndDatetime = "";
            financeSalesDetailRptQueryEntiry.SHOW_DATE = "N";
            this.BindData2gvDiffCountReport(financeSalesDetailRptQueryEntiry);
        }

        protected void btnRefresh_Click(object sender, EventArgs e)
        {
            FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry = this.GetFinanceSalesDetailRptQueryEntiry();
            this.BindData2gvDiffCountReport(financeSalesDetailRptQueryEntiry);
        }

        private void CreateGridView(DataSet ds, string byCustomer)
        {
            int i;
            BoundField bf;
            int iwidth = 0x578;
            if (byCustomer == "Y")
            {
                if (ds.Tables.Count > 0)
                {
                    for (i = 11; ds.Tables[0].Columns.Count > i; i++)
                    {
                        bf = new BoundField {
                            HeaderText = ds.Tables[0].Columns[i].ToString(),
                            DataField = ds.Tables[0].Columns[i].ToString(),
                            SortExpression = ds.Tables[0].Columns[i].ToString()
                        };
                        this.gvFinaceSalesDetailReportByCustomer.Columns.Add(bf);
                        iwidth += 100;
                        this.gvFinaceSalesDetailReportByCustomer.Width = iwidth;
                    }
                }
            }
            else
            {
                for (i = 11; (ds.Tables[0].Columns.Count - 1) > i; i++)
                {
                    bf = new BoundField {
                        HeaderText = ds.Tables[0].Columns[i].ToString(),
                        DataField = ds.Tables[0].Columns[i].ToString(),
                        SortExpression = ds.Tables[0].Columns[i].ToString()
                    };
                    this.gvFinanceSalesDetailReport.Columns.Add(bf);
                    iwidth += 100;
                    this.gvFinanceSalesDetailReport.Width = iwidth;
                }
            }
        }

        private FinanceSalesDetailRptQueryEntiry GetFinanceSalesDetailRptQueryEntiry()
        {
            return (base.PageQueryEntitySession as FinanceSalesDetailRptQueryEntiry);
        }

        protected void gvFinaceSalesDetailReportByCustomer_Init(object sender, EventArgs e)
        {
        }

        protected void gvFinaceSalesDetailReportByCustomer_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                if (((Label) e.Row.FindControl("lblQTY")) != null)
                {
                    this.TotalQty += decimal.Parse((((Label) e.Row.FindControl("lblQTY")).Text != "") ? ((Label) e.Row.FindControl("lblQTY")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblUNIT_PRICE")) != null)
                {
                    this.TotalUnitPrice += decimal.Parse((((Label) e.Row.FindControl("lblUNIT_PRICE")).Text != "") ? ((Label) e.Row.FindControl("lblUNIT_PRICE")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblEXTENDED_PRICE")) != null)
                {
                    this.TotalExtendedPrice += decimal.Parse((((Label) e.Row.FindControl("lblEXTENDED_PRICE")).Text != "") ? ((Label) e.Row.FindControl("lblEXTENDED_PRICE")).Text : "0");
                }
            }
        }

        protected void gvFinaceSalesDetailReportByCustomer_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry = this.GetFinanceSalesDetailRptQueryEntiry();
                financeSalesDetailRptQueryEntiry.SortField = e.SortExpression;
                if (financeSalesDetailRptQueryEntiry.SortDirection == "ASC")
                {
                    financeSalesDetailRptQueryEntiry.SortDirection = "DESC";
                }
                else if (financeSalesDetailRptQueryEntiry.SortDirection == "DESC")
                {
                    financeSalesDetailRptQueryEntiry.SortDirection = "ASC";
                }
                this.BindData2gvDiffCountReport(financeSalesDetailRptQueryEntiry);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        protected void gvFinanceSalesDetailReport_Init(object sender, EventArgs e)
        {
        }

        protected void gvFinanceSalesDetailReport_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (e.Row.RowIndex + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                if (this.hdnActionID.Value != "EXPORT")
                {
                    base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblSkuDescr"));
                }
                if (((Label) e.Row.FindControl("lblQTY")) != null)
                {
                    this.TotalQty += decimal.Parse((((Label) e.Row.FindControl("lblQTY")).Text != "") ? ((Label) e.Row.FindControl("lblQTY")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblUNIT_PRICE")) != null)
                {
                    this.TotalUnitPrice += decimal.Parse((((Label) e.Row.FindControl("lblUNIT_PRICE")).Text != "") ? ((Label) e.Row.FindControl("lblUNIT_PRICE")).Text : "0");
                }
                if (((Label) e.Row.FindControl("lblEXTENDED_PRICE")) != null)
                {
                    this.TotalExtendedPrice += decimal.Parse((((Label) e.Row.FindControl("lblEXTENDED_PRICE")).Text != "") ? ((Label) e.Row.FindControl("lblEXTENDED_PRICE")).Text : "0");
                }
            }
        }

        protected void gvFinanceSalesDetailReport_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry = this.GetFinanceSalesDetailRptQueryEntiry();
                financeSalesDetailRptQueryEntiry.SortField = e.SortExpression;
                if (financeSalesDetailRptQueryEntiry.SortDirection == "ASC")
                {
                    financeSalesDetailRptQueryEntiry.SortDirection = "DESC";
                }
                else if (financeSalesDetailRptQueryEntiry.SortDirection == "DESC")
                {
                    financeSalesDetailRptQueryEntiry.SortDirection = "ASC";
                }
                this.BindData2gvDiffCountReport(financeSalesDetailRptQueryEntiry);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry = new FinanceSalesDetailRptQueryEntiry();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    financeSalesDetailRptQueryEntiry.OwnerID = this.PreviousPage.OwnerID;
                    financeSalesDetailRptQueryEntiry.CustomerID = this.PreviousPage.CustomerID;
                    financeSalesDetailRptQueryEntiry.SkuID = this.PreviousPage.SkuID;
                    financeSalesDetailRptQueryEntiry.StartDatetime = this.PreviousPage.StartDatetime;
                    financeSalesDetailRptQueryEntiry.EndDatetime = this.PreviousPage.EndDatetime;
                    financeSalesDetailRptQueryEntiry.SHOW_DATE = this.PreviousPage.ShowDate;
                    financeSalesDetailRptQueryEntiry.BY_CUSTOMER = this.PreviousPage.ByCustomer;
                }
                else
                {
                    financeSalesDetailRptQueryEntiry.OwnerID = "";
                    financeSalesDetailRptQueryEntiry.CustomerID = "";
                    financeSalesDetailRptQueryEntiry.SkuID = "";
                    financeSalesDetailRptQueryEntiry.StartDatetime = "";
                    financeSalesDetailRptQueryEntiry.EndDatetime = "";
                    financeSalesDetailRptQueryEntiry.SHOW_DATE = "N";
                    financeSalesDetailRptQueryEntiry.BY_CUSTOMER = "Y";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            financeSalesDetailRptQueryEntiry.IsGetAll = false;
            financeSalesDetailRptQueryEntiry.CurrentPage = "0";
            financeSalesDetailRptQueryEntiry.SortField = "OD.SKU_ID";
            financeSalesDetailRptQueryEntiry.SortDirection = "DESC";
            if (financeSalesDetailRptQueryEntiry.BY_CUSTOMER == "Y")
            {
                financeSalesDetailRptQueryEntiry.PageSize = this.gvFinaceSalesDetailReportByCustomer.PageSize.ToString();
            }
            else
            {
                financeSalesDetailRptQueryEntiry.PageSize = this.gvFinanceSalesDetailReport.PageSize.ToString();
            }
            this.SetFinanceSalesDetailRptQueryEntiry(financeSalesDetailRptQueryEntiry);
            this.BindData2gvDiffCountReport(financeSalesDetailRptQueryEntiry);
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                base.InitAnalyticsReportGridview(this.gvFinanceSalesDetailReport);
                this.LoadData();
            }
        }

        public void SetFinanceSalesDetailRptQueryEntiry(FinanceSalesDetailRptQueryEntiry financeSalesDetailRptQueryEntiry)
        {
            base.PageQueryEntitySession = financeSalesDetailRptQueryEntiry;
        }

        public FinanceSalesDetailRptQuery PreviousPage
        {
            get
            {
                return (FinanceSalesDetailRptQuery) base.PreviousPage;
            }
        }
    }
}

